package com.venta.autopartes.vehicle.search;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.venta.autopartes.car.Vehicle;
import com.venta.autopartes.dao.UserDAO;
import com.venta.autopartes.dao.VehicleDAO;
import com.venta.autopartes.menu.LeftFloatingMenu;
import com.venta.autopartes.user.User;
import com.venta.autopartes.utils.Constants;
import com.venta.autopartes.utils.DataCatalog;
import com.venta.autopartes.utils.SessionFactory;
import com.venta.autopartes.utils.SessionValidator;

public class SearchVehiclesAction extends ActionSupport implements SessionAware{
	private Map session;
	private int carId;
	private DataCatalog dataCatalog;
	
	private Vehicle vehicleForm;
	
	public String searchMyVehicles(){
		List<Vehicle> vehiclesListSearch=null;
		String result=INPUT;
		
		if(!SessionValidator.isValidSession(session)){
			return SessionValidator.INVALID_SESSION;
		}
		
		if(session.get("vehiclesListSearch")==null){
			SessionFactory.removePreviosSessionData();
			User user=(User)session.get(Constants.USER_LOGGED);
			
			VehicleDAO vehicleDAO= new VehicleDAO();
			
			vehiclesListSearch=vehicleDAO.getVehiclesByUser(user.getUserId());
			session.put("vehiclesListSearch", vehiclesListSearch);
			
			result=SUCCESS;
		}else{
			return SUCCESS;
		}
		
		return result;
	}
	
	public String searchVehicleById(){
		Vehicle vehicleSearchResult=null;
		String result=INPUT;
		
		System.out.println("carId : " + carId + " , is going to be searched.");
		
		List<Vehicle> vehiclesListSearch=(List<Vehicle>)session.get("vehiclesListSearch");
		
		System.out.println("Is there VehicleListSearch in session?--> " +vehiclesListSearch);
		
		for(Vehicle vehicle:vehiclesListSearch){
			if (vehicle.getCarId()==carId){
				System.out.println("The vehicle with id: " + carId + " was found --> " + vehicle.getMake() );
				vehicleSearchResult=vehicle;
				session.put("vehicleSearchResult", vehicleSearchResult);
			}
		}
		
		result=SUCCESS;
		
		return result;
	}
	
	public String deleteVehicleById(){
		Vehicle vehicleSearchResult=null;
		String result=INPUT;
		
		System.out.println("carId : " + carId + " , is going to be removed.");
		
		List<Vehicle> vehiclesListSearch=(List<Vehicle>)session.get("vehiclesListSearch");
		
		System.out.println("Is there VehicleListSearch in session?--> " +vehiclesListSearch);
		
		for(Vehicle vehicle:vehiclesListSearch){
			if (vehicle.getCarId()==carId){
				VehicleDAO vehicleDAO=new VehicleDAO();
				vehicleDAO.deleteVehicleById(vehicle.getCarId());
				System.out.println("The vehicle with id: " + carId + " was found and it will be removed --> " + vehicle.getMake() );
				vehicleSearchResult=vehicle;
				
			}
		}
		if(vehicleSearchResult!=null){
			vehiclesListSearch.remove(vehicleSearchResult);
		}
		
		result=SUCCESS;
		
		return result;
	}
	
	public String goUpdateVehicleDetailsById(){
		Vehicle vehicleSearchResult=null;
		String result=INPUT;
		
		System.out.println("carId : " + carId + " , is going to be searched.");
		
		List<Vehicle> vehiclesListSearch=(List<Vehicle>)session.get("vehiclesListSearch");
		
		System.out.println("Is there VehicleListSearch in session?--> " +vehiclesListSearch);
		
		if(vehiclesListSearch!=null){
			for(Vehicle vehicle:vehiclesListSearch){
				if (vehicle.getCarId()==carId){
					System.out.println("The vehicle with id: " + carId + " was found --> " + vehicle.getMake() );
					vehicleSearchResult=vehicle;
				}
			}
			session.put("vehicle", vehicleSearchResult);
			vehicleForm=vehicleSearchResult;
		}
		
		result=SUCCESS;
		
		return result;
	}
	
	
	

	public Vehicle getVehicleForm() {
		return vehicleForm;
	}

	public void setVehicleForm(Vehicle vehicleForm) {
		this.vehicleForm = vehicleForm;
	}

	public int getCarId() {
		return carId;
	}

	public void setCarId(int carId) {
		this.carId = carId;
	}
	
	

	public DataCatalog getDataCatalog() {
		return dataCatalog;
	}

	public void setDataCatalog(DataCatalog dataCatalog) {
		this.dataCatalog = dataCatalog;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}
	
}
